package cn.kilo.domain.service.user.impl;

import cn.kilo.domain.model.login.vo.LoginVo;
import cn.kilo.domain.model.login.vo.RegisterVO;
import cn.kilo.domain.model.login.vo.UserVO;
import cn.kilo.domain.model.order.entity.OrderEntity;
import cn.kilo.domain.model.order.entity.StaffEntity;
import cn.kilo.domain.model.order.entity.UserEntity;
import cn.kilo.domain.model.order.res.JudgeOfProblemRes;
import cn.kilo.domain.repository.IStrategyRepository;
import cn.kilo.domain.service.infterface.IRoleJob;
import cn.kilo.domain.service.user.IUserService;
import cn.kilo.domain.support.OssUploadService;
import cn.kilo.domain.support.SnowFlake;
import cn.kilo.types.common.Constants;
import cn.kilo.types.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author kilok
 */
@Service
@Slf4j
public class UserService implements IUserService {

    @Resource
    private IStrategyRepository repository;
    @Resource
    private SnowFlake snowFlake;
    @Resource
    private OssUploadService ossUploadService;
    @Resource
    private IRoleJob roleJob;

    @Override
    public Response<?> login(LoginVo loginReq) {
        return repository.login(loginReq);
    }

    @Override
    public boolean register(RegisterVO registerVO) {
        registerVO.setUserId(String.valueOf(snowFlake.nextId()));
        return repository.register(registerVO);
    }

    @Override
    public Boolean updateUserInfo(UserVO userVO) {
        return repository.updateUserInfo(userVO);
    }

    @Override
    public String updateUserImage(MultipartFile image, String userId) {
        try {
            String url = ossUploadService.uploadImg(image);
            repository.saveUserImage(url, userId);
            return url;
        } catch (IOException e) {
            log.info("图片上传失败");
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getUserRole(String userId) {
        return repository.getUserRole(userId);
    }

    @Override
    public Boolean beStaff(StaffEntity staffEntity) {

        return roleJob.beStaff(staffEntity);
    }

    @Override
    public boolean setLocation(UserEntity entity) {
        String[] split=entity.getRoomNumber().split(Constants.UNDERLINE);
        entity.setRidgepole(split[0]);
        entity.setFloor(Integer.valueOf(split[1]));
        entity.setRoomNumber(split[2]);
        return repository.setLocation(entity);
    }

    @Override
    public StaffEntity registerToStaff(UserEntity build) {
        log.info("传入参数信息:{}",build);
        StaffEntity staffEntity=StaffEntity.builder()
                .staffId(build.getUserId())
                .phoneNumber(build.getPhoneNumber())
                .payAccount(build.getPayAccount())
                .staffName(build.getUserName())
                .payNickName(build.getPayNickName())
                .build();
        return repository.registerToStaff(staffEntity);
    }

    @Override
    public JudgeOfProblemRes problem(UserEntity build) {
        build.setJudgeId(String.valueOf(snowFlake.nextId()));
        return repository.problem(build);
    }

    @Override
    public String problemPicture(String judgeId, MultipartFile image) throws IOException {
        String url = ossUploadService.uploadImg(image);
        boolean b = repository.updateProlemPicture(judgeId, url);
        if(b){
            return url;
        }
        return null;
    }

    @Override
    public boolean setPost(UserEntity build) {
        return repository.setPost(build);
    }

    @Override
    public void getAllOrder(OrderEntity build) {
        repository.getAllOrder(build);
    }

}
